package com.ginmeta.controller.file;

import com.ginmeta.common.base.AjaxResult;
import com.ginmeta.common.base.BaseController;
import com.ginmeta.common.config.SystemConfig;
import com.ginmeta.common.utils.file.FileUploadUtils;
import com.ginmeta.common.utils.file.MimeTypeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件
 */
@Slf4j
@RestController
@RequestMapping("/file")
public class FileController extends BaseController {

    /**
     * 上传
     * @param file 文件
     * @return 结果
     */
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            // 上传
            String url = FileUploadUtils.upload(SystemConfig.getProfile() + "/file", file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
            return AjaxResult.success("上传成功", url);
        }
        return AjaxResult.error("上传异常，请联系管理员");
    }
}
